﻿using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations;

namespace Yz.Base.Dal
{
    public class MigrateDBToLatestVersion<TContext, TMigrationsConfiguration> : IDatabaseInitializer<TContext>
          where TContext : DbContext
          where TMigrationsConfiguration : DbMigrationsConfiguration<TContext>, new()
    {
        private readonly DbMigrationsConfiguration _config;
        public MigrateDBToLatestVersion(string connectionString)
        {
            TMigrationsConfiguration tMigrationsConfiguration = Activator.CreateInstance<TMigrationsConfiguration>();
            tMigrationsConfiguration.TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient");
            _config = tMigrationsConfiguration;
        }
        public virtual void InitializeDatabase(TContext context)
        {
            DbMigrator dbMigrator = new DbMigrator(_config);
            dbMigrator.Update();
        }
    }
}
